package code;
import java.util.ArrayList;

import code.IOperatorDAO.DALException;

public class BaseController{

	private IOperatorDAO 	DAO;
	private IUI				 	UI;
	ArrayList<String>			options = new ArrayList<String>();
	private OperatorDTO	operator;
	
	public BaseController(IUI UI, IOperatorDAO d){
		this.DAO = d;
		this.UI	= UI;
	}
	
	public boolean isLoggedIn()
	{
		return (operator!=null);
	}
	
	public void setOperator(OperatorDTO opr) throws DALException
	{
		if (DAO.getOperatorList().indexOf(opr) > -1)
		{
			operator = opr;
		} else
		{
			throw new DALException("Operatør findes ikke");
		}
	}
	
	// The system's login function, takes operator's pw and ID, and checks against the database
	// to verify
	public void login()
	{
		String login = UI.getLogin();
		int oprId;
		String password;

		int split = login.indexOf(".");
		oprId = Integer.parseInt(login.substring(0, split));
		password = login.substring(split+1);

		try
		{
			setOperator(DAO.getOperator(oprId, password));
			operations();
		} catch (DALException e) {
			UI.printError(e.getMessage());
			login();
		}

	}

	public void operations()
	{
		//		Metoder der udfra operatørID, skal kunne tilføre funktioner
		//      til UI.printMenu, og herefter kalde de metoder der er behov for.
		//      En operatør skal kunne ændre sit password, og skal kunne benytte vægten.
	
		ArrayList<String> options = new ArrayList<String>();

		options.add("1 Ændre password");  				// tast 1
		options.add("2 Benyt vægt");      				// tast 2

		if (operator.getID() == 10){

			options.add("3 Opret operatør");   			// tast 3
			options.add("4 Operatør liste");   			// tast 4
			options.add("5 Ændre operatør data");   	// tast 5
			options.add("6 Slet operatør");	   			// tast 6

		}
		options.add("0 Log ud");    			      			// tast 0

		int choice = UI.printMenu(options, operator.getName());

		switch (choice){
		// Ændr Password
		case 1 :  changePassword();
			break;
		//	Benyt vægt
		case 2 : useWeight();
			break;
			// opret operatør
		case 3: createOperator();
			break;
			// Operatør liste
		case 4 :printOperators();
			break;
			// Operatør liste
		case 5 :changeOperatorData();
			break;
			// Operatør liste
		case 6: deleteOperator();
			break;
			// log ud
		case 0:
			logout();
			login();
			break;
		default:

		}

	}
	
	// Logs user out the system
	public void logout()
	{
		operator = null;
	}

	// Method for using the weight
	private void useWeight(){
		String bruttoWeight, taraWeight;
		try {
			String weights = UI.receiveWeight();
			int subWeights = weights.indexOf("&");

			bruttoWeight	= weights.substring(0, subWeights);
			taraWeight		= weights.substring(subWeights+1);

			double dTara, dBrutto;

			dTara = Double.parseDouble(taraWeight);
			dBrutto = Double.parseDouble(bruttoWeight);

			IWeightController weightController = new WeightController(); 
			UI.weightResult(weightController.getNetto(dTara, dBrutto));
		} catch (DALException e) {
			UI.printError(e.getMessage());
		}

		operations();	
	}

	private void createOperator(){
		String newOperator = UI.createOperatorDialog();
		String oprName = newOperator.substring(0, newOperator.indexOf('.'));
		String ini= newOperator.substring(newOperator.indexOf('.') + 1,newOperator.lastIndexOf('.'));
		String cpr = newOperator.substring(newOperator.lastIndexOf('.') + 1);
		try {
			OperatorDTO newOpr = DAO.createOperator(oprName, ini, cpr);
			UI.printInfo("Bruger oprettet med ID " + newOpr.getID() + " og passwordet: " + newOpr.getPassword());
		} catch (DALException e) {
			UI.printError(e.getMessage());
		}
		operations();
	}
	
	// Giver en liste med alle operatører
	private void printOperators(){
		UI.printAllOperators(DAO.getOperatorList());
		operations();
	}
	
	private void changePassword(){
		// Menu valg 1 -  Ændrer password
		try {
			DAO.updateOperator(operator, UI.newPasswordDialog(operator.getPassword()));
			for (OperatorDTO operators : DAO.getOperatorList())
			{
				if (operators.getID() == operator.getID())
				{
					operator = operators;
				}
			}
		} catch (DALException e) {
			UI.printError(e.getMessage());
		}
		operations();
	}

	private void changeOperatorData()
	{
		String[] split = UI.changeOperatorData().split("&");
		try {
			DAO.changeOperatorData(Integer.parseInt(split[0]), split[1], split[2], split[3]);
		} catch (Exception e)
		{
			UI.printError(e.getMessage() + " (God fejl!)");
		}
		operations();
	}
	
	private void deleteOperator()
	{
		try
		{
			DAO.deleteOperator(UI.deleteOperator());
		}
		catch (DALException e) 
		{
			UI.printError(e.getMessage());
		}
		
		operations();
	}
}